<template>
  <div class="app-container">
    <el-card class="box-card">
      <h3>{{$t('Order_details')}}</h3>
      <!-- <div>
        <el-steps :active="formatStepStatus(orderDetail.order.orderStatus)" finish-status="error" align-center v-if="orderDetail.order.orderStatus == 203">
          <el-step>
            <span slot="title">{{$t('Refunded')}}</span>
            <span v-if="orderDetail.order.orderStatus == 203" slot="description">{{orderDetail.order.updateTime}}</span>
          </el-step>
        </el-steps>
        <el-steps v-else :active="formatStepStatus(orderDetail.order.orderStatus)" finish-status="success" align-center>
          <el-step>
            <span slot="title">{{$t('Submit_order')}}</span>
            <span v-if="orderDetail.order.orderStatus == 101" slot="description">{{orderDetail.order.updateTime}}</span>
          </el-step>
          <el-step>
            <span slot="title">{{$t('Pay_for_order')}}</span>
            <span v-if="orderDetail.order.orderStatus == 201" slot="description">{{orderDetail.order.updateTime}}</span>
          </el-step>
          <el-step title="">
            <span slot="title">{{$t('Send_via_platform')}}</span>
            <span v-if="orderDetail.order.orderStatus == 301" slot="description">{{orderDetail.order.updateTime}}</span>
          </el-step>
          <el-step title="">
            <span slot="title">{{$t('Receive_confirm')}}</span>
            <span v-if="orderDetail.order.orderStatus == 401" slot="description">{{orderDetail.order.updateTime}}</span>
          </el-step>
          <el-step title="">
            <span slot="title">{{$t('Finish_Rating_')}}</span>
            <span v-if="orderDetail.order.orderStatus == 501" slot="description">{{orderDetail.order.updateTime}}</span>
          </el-step>
        </el-steps>
      </div> -->

      <el-form :inline="true">
        <el-col :span="14">
          <el-form-item :label="$t('Current_status')">
            {{ orderStatusFilter(orderDetail.order.orderStatus)  }}
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="">
            <el-button icon="el-icon-s-promotion" type="success" plain1 v-if="orderDetail.order.orderStatus == 201" @click="consignment(orderDetail.order)">{{$t('Processing_delivery')}}</el-button>
            <el-button icon="el-icon-s-order" type="warning" plain1 @click="addMark(orderDetail.order)">{{$t('Add_Notes')}}</el-button>
            <el-button icon="el-icon-printer" type="info" plain1 @click="print(orderDetail.order)">{{$t('打印订单')}}</el-button>
          </el-form-item>
        </el-col>
      </el-form>

      <div style="margin-top: 20px">
        <svg-icon icon-class="marker" style="color: #606266"/>
        <span class="font-small">{{$t('Basic_Information')}}</span>
      </div>
      <div class="table-layout">
        <el-row>
          <el-col :span="6" class="table-cell-title">{{$t('Order_ID')}}</el-col>
          <el-col :span="6" class="table-cell-title">{{$t('Delivery_number')}}</el-col>
          <el-col :span="6" class="table-cell-title">{{$t('Member_account')}}</el-col>
          <el-col :span="6" class="table-cell-title">{{$t('Delivery_Company')}}</el-col>
        </el-row>
        <el-row>
          <el-col :span="6" class="table-cell">{{ orderDetail.order.orderSn }}</el-col>
          <el-col :span="6" class="table-cell" v-if="orderDetail.order.shipSn">{{ orderDetail.order.shipSn }}</el-col>
          <el-col :span="6" class="table-cell" v-else></el-col>
          <el-col :span="6" class="table-cell">{{ orderDetail.user ? orderDetail.user.nickname : "" }}</el-col>
          <el-col :span="6" class="table-cell">{{ orderDetail.order.shipChannel }}</el-col>
        </el-row>

        <el-row>
          <el-col :span="6" class="table-cell-title">{{$t('Payment_method')}}</el-col>
          <el-col :span="6" class="table-cell-title">{{$t('Source_of_order')}}</el-col>
          <el-col :span="6" class="table-cell-title">{{$t('Order_type')}}</el-col>
          <el-col :span="6" class="table-cell-title">{{$t('Order_remark')}}</el-col>
        </el-row>
        <el-row>
          <el-col :span="6" class="table-cell">{{ payTypeFilter(orderDetail.order.payType) }}</el-col>
          <el-col :span="6" class="table-cell">{{ orderSourceFilter(orderDetail.order.orderSource)  }}</el-col>
          <el-col :span="6" class="table-cell">{{ orderTypeFilter(orderDetail.order.orderType)  }}</el-col>
          <el-col :span="6" class="table-cell">{{ orderDetail.order.message }}</el-col>
        </el-row>

        <el-row>
          <el-col :span="6" class="table-cell-title">{{$t('订单名称')}}</el-col>
          <el-col :span="6" class="table-cell-title">{{$t('完成时间')}}</el-col>
          <el-col v-if="orderDetail.order.giftCardNo" :span="6" class="table-cell-title">{{$t('礼物卡号')}}</el-col>
        </el-row>
        <el-row>
          <el-col :span="6" class="table-cell">{{ orderDetail.order.name }}</el-col>
          <el-col :span="6" class="table-cell">{{ orderDetail.order.completeTime }}</el-col>
          <el-col v-if="orderDetail.order.giftCardNo" :span="6" class="table-cell">{{ orderDetail.order.giftCardNo }}</el-col>
        </el-row>
      </div>

      <div style="margin-top: 20px">
        <svg-icon icon-class="marker" style="color: #606266"/>
        <span class="font-small">{{$t('收货信息')}}</span>
      </div>
      <div class="table-layout">
        <el-row>
          <el-col :span="8" class="table-cell-title">{{$t('Receiver_')}}</el-col>
          <el-col :span="8" class="table-cell-title">{{$t('Cellphone_number')}}</el-col>
          <el-col :span="8" class="table-cell-title">{{$t('Receiving_Address')}}</el-col>
        </el-row>
        <el-row>
          <el-col :span="8" class="table-cell">{{ orderDetail.order.consignee }}</el-col>
          <el-col :span="8" class="table-cell">{{ orderDetail.order.mobile }}</el-col>
          <el-col :span="8" class="table-cell">{{ orderDetail.order.address }}</el-col>
        </el-row>
      </div>
      <div style="margin-top: 20px">
        <svg-icon icon-class="marker" style="color: #606266"/>
        <span class="font-small">{{$t('Goods_details_info')}}</span>
      </div>
      <div class="table-layout">
        <el-table :data="orderDetail.goodsLs" border fit highlight-current-row>
          <el-table-column align="center" :label="$t('商品名称')" prop="goodsName" />
          <el-table-column align="center" :label="$t('Merchandise_code')" prop="goodsSn" />
          <el-table-column align="center" :label="$t('Goods_specifications')" prop="specifications" />
          <el-table-column align="center" :label="$t('商品辅料')" prop="accessory" />
          <el-table-column align="center" :label="$t('商品属性')" prop="property" />
          <el-table-column align="center" :label="$t('Products')" prop="number">
            <template slot-scope="scope">
                <div>{{scope.row.number}}</div>
                <div v-if="scope.row.refundNumber > 0">{{$t('Refunded')}}{{scope.row.refundNumber}}</div>
            </template>
          </el-table-column>
          <el-table-column align="center" :label="$t('单价')" prop="sellingPrice">
            <template slot-scope="scope">
             $  {{scope.row.sellingPrice | floorTwoDecimal}}
            </template>
          </el-table-column>
          <el-table-column align="center" :label="$t('优惠金额')" >
            <template slot-scope="scope">
                $ {{scope.row.sellingPrice - scope.row.discountPrice | floorTwoDecimal}}
            </template>
          </el-table-column>
          <el-table-column align="center" :label="$t('实付单价')" prop="discountPrice">
            <template slot-scope="scope">
              $ {{scope.row.discountPrice | floorTwoDecimal}}
            </template>
          </el-table-column>
          <el-table-column align="center" :label="$t('税前总金额')" prop="discountGoodsPrice">
            <template slot-scope="scope">
              $ {{scope.row.discountGoodsPrice | floorTwoDecimal}}
            </template>
          </el-table-column>
          <el-table-column align="center" :label="$t('税费')" prop="taxs">
            <template slot-scope="scope">
              <div>$ {{scope.row.taxPrice | floorTwoDecimal}}</div>
              <div v-for="tax in scope.row.taxs" :key="tax.id">
                $ {{tax.code}}({{tax.value}}%)：{{tax.price | floorTwoDecimal}}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" :label="$t('合计')" prop="price">
            <template slot-scope="scope">
              $ {{scope.row.price | floorTwoDecimal}}
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div style="margin-top: 20px">
        <svg-icon icon-class="marker" style="color: #606266"/>
        <span class="font-small">{{$t('Cost_details')}}</span>
      </div>
      <div class="table-layout">
        <el-row>
          <el-col :span="8" class="table-cell-title">{{$t('Total')}}</el-col>
          <el-col :span="8" class="table-cell-title">{{$t('Delivery_fee')}}</el-col>
          <el-col :span="8" class="table-cell-title">{{$t('优惠金额')}}</el-col>
          <!-- <el-col :span="6" class="table-cell-title">{{$t('Points_discount')}}</el-col> -->
        </el-row>
        <el-row>
          <el-col :span="8" class="table-cell">{{ orderDetail.order.goodsPrice | floorTwoDecimal }}</el-col>
          <el-col :span="8" class="table-cell">{{ orderDetail.order.freightPrice | floorTwoDecimal }}</el-col>
          <el-col :span="8" class="table-cell">{{ orderDetail.order.couponPrice | floorTwoDecimal }}</el-col>
          <!-- <el-col :span="6" class="table-cell">{{ orderDetail.order.integralPrice }}</el-col> -->
        </el-row>
        <el-row>
          <el-col :span="8" class="table-cell-title">{{$t('税费')}}</el-col>
          <el-col :span="8" class="table-cell-title">{{$t('税前总金额')}}</el-col>
          <el-col :span="8" class="table-cell-title">{{$t('Total_to_be_paid')}}</el-col>
        </el-row>
        <el-row>
          <el-col :span="8" class="table-cell">{{ orderDetail.order.taxPrice | floorTwoDecimal }}</el-col>
          <el-col :span="8" class="table-cell">{{ orderDetail.order.goodsPrice | floorTwoDecimal }}</el-col>
          <el-col :span="8" class="table-cell">{{ orderDetail.order.orderPrice | floorTwoDecimal }}</el-col>
        </el-row>
      </div>

      <div style="margin-top: 20px">
        <svg-icon icon-class="marker" style="color: #606266"/>
        <span class="font-small">{{$t('Notes_info')}}</span>
      </div>
      <div class="table-layout">
        <el-tag :style="{'margin':'20px'}" v-for="(remark,index) in orderRemarks" effect="dark">{{'备注'+(index+1) +'、'+remark.remark}}</el-tag>
      </div>
    </el-card>

    <!-- 发货对话框 -->
    <el-dialog :visible.sync="shipDialogVisible" :title="$t('Delivery_')">
      <el-form ref="shipForm" :model="shipForm" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item :label="$t('Express_Delivery_Company')" prop="shipChannel">
          <el-input v-model="shipForm.shipChannel"/>
        </el-form-item>
        <el-form-item :label="$t('Express_Delivery_number')" prop="shipSn">
          <el-input v-model="shipForm.shipSn"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="shipDialogVisible = false">{{$t('Cancel')}}</el-button>
        <el-button type="primary" @click="confirmShip">{{$t('Confirm')}}</el-button>
      </div>
    </el-dialog>

    <!-- 添加备注对话框 -->
    <el-dialog :visible.sync="markDialogVisible" :title="$t('Notes')">
      <el-form ref="markForm" :model="markForm" :rules="markRules" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item :label="$t('Notes')" prop="remark">
          <el-input v-model="markForm.remark" type="textarea"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="markDialogVisible = false">{{$t('Cancel')}}</el-button>
        <el-button type="primary" @click="confirmMark">{{$t('Confirm')}}</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { shipOrder, detailOrder, remarkOrder, orderGetRemarks } from '@/api/order'

export default {
  name: 'orderDetail',
  filters: {


  },
  data() {
    const statusMap = {
      0: this.$t('新建'),
      1: this.$t('Hangup'),
      2: this.$t('未支付'),
      3: this.$t('未发货'),
      4: this.$t('运输中'),
      5: this.$t('完成'),
      6: this.$t('退款')
    }

    const payTypeMap = {
      1: this.$t('现金支付'),
      2: this.$t('礼物卡支付'),
      3: this.$t('PayPal支付'),
      4: this.$t('余额支付'),
      5: this.$t('Clover支付'),
      6: this.$t('积分支付')
    }

    const orderSourceMap = {
      1: this.$t('手机APP'),
      2: this.$t('门店下单'),
      3: this.$t('官网商城'),
      4: this.$t('客户订阅')
    }

    const orderTypeMap = {
      1: this.$t('自提'),
      2: this.$t('配送'),
      3: this.$t('堂食'),
      4: this.$t('外带')
    }

    return {
      statusMap,
      payTypeMap,
      orderSourceMap,
      orderTypeMap,
      orderDetail: {
        order: {},
        user: {},
        orderGoods: []
      },
      orderRemarks: [],
      shipForm: {
        orderId: undefined,
        shipChannel: undefined,
        shipSn: undefined
      },
      shipDialogVisible: false,
      markDialogVisible: false,
      markForm: {
        orderIdd: undefined,
        remark: undefined
      },
      markRules: {
        remark: [
          { required: true, message: this.$t('Notes_cannot_be_empty'), trigger: 'blur' }
        ],
      },
    }
  },
  created() {
    this.getDetail()
  },
  methods: {
    orderStatusFilter(status) {
      return this.statusMap[status]
    },
    payTypeFilter(type){
      return this.payTypeMap[type]
    },
    orderSourceFilter(source){
      return this.orderSourceMap[source]
    },
    orderTypeFilter(type){
      return this.orderTypeMap[type]
    },
    getDetail() {
      const id = this.$route.query.id
      detailOrder(id).then(response => {
        this.orderDetail = response.data.data
      })
      this.getRemarks();
    },
    getRemarks() {
      const id = this.$route.query.id
      orderGetRemarks(id).then(response => {
        this.orderRemarks = response.data.data.list
      })
    },
    formatStepStatus(value) {
      if (value === 201) {
        // 待发货
        return 2
      } else if (value === 301) {
        // 已发货
        return 3
      } else if (value === 401 || value === 402) {
        // 已完成
        return 4
      } else {
        // 待付款、已关闭、无限订单
        return 1
      }
    },
    consignment(row) {
      this.shipForm.orderId = row.id
      this.shipForm.shipChannel = row.shipChannel
      this.shipForm.shipSn = row.shipSn

      this.shipDialogVisible = true
      this.$nextTick(() => {
        this.$refs['shipForm'].clearValidate()
      })
    },
    print(order){
      this.$router.push({path:'/order/print',query:{id:order.id}})
    },
    addMark(row) {
      this.markDialogVisible = true
      this.markForm.orderId = row.id

    },
    confirmShip() {
      this.$refs['shipForm'].validate((valid) => {
        if (valid) {
          shipOrder(this.shipForm).then(response => {
            this.shipDialogVisible = false
            this.$notify.success({
              title: this.$t('Success!'),
              message: this.$t('Confirm_delivery_successful')
            })
          }).catch(response => {
            this.$notify.error({
              title: this.$t('Failed'),
              message: this.$t(response.data.errmsg)
            })
          })
        }
      })
    },
    confirmMark() {
      this.$refs['markForm'].validate((valid)=>{
        if(valid){
          remarkOrder(this.markForm).then(response => {
            this.markDialogVisible = false
            this.$notify.success({
              title: this.$t('Success!'),
              message: this.$t('Noted!')
            })
            this.getRemarks()
          }).catch(response => {
            this.$notify.error({
              title: this.$t('Failed'),
              message: this.$t(response.data.errmsg)
            })
          })
        }
      })
    }
  }
}
</script>

<style scoped>
  .detail-container {
    width: 80%;
    padding: 20px 20px 20px 20px;
    margin: 20px auto;
  }

  .operate-container {
    background: #F2F6FC;
    height: 80px;
    margin: -20px -20px 0;
    line-height: 80px;
  }

  .operate-button-container {
    float: right;
    margin-right: 20px
  }

  .table-layout {
    margin-top: 20px;
    border-left: 1px solid #DCDFE6;
    border-top: 1px solid #DCDFE6;
  }

  .table-cell {
    height: 60px;
    line-height: 40px;
    border-right: 1px solid #DCDFE6;
    border-bottom: 1px solid #DCDFE6;
    padding: 10px;
    font-size: 14px;
    color: #606266;
    text-align: center;
    overflow: hidden;
  }

  .table-cell-title {
    border-right: 1px solid #DCDFE6;
    border-bottom: 1px solid #DCDFE6;
    padding: 10px;
    background: #F2F6FC;
    text-align: center;
    font-size: 14px;
    color: #303133;
  }
</style>
